package com.example.paper.http.retrofit;

import com.example.paper.api.Api;
import com.example.paper.http.factory.HttpImpl;
import com.example.paper.interceptor.SignInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class UploadRetrofit implements HttpImpl {

    private static Retrofit retrofit;

    //无参
    private UploadRetrofit() {
    }

    @Override
    public Retrofit getRetrofit() {
        return retrofit;
    }

    //静态方法调用网络请求
    public static class Build {
        public Build() {
            createRetrofit();
        }

        private void createRetrofit() {

            //拦截
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            //网络请求
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.addInterceptor(interceptor);
//            builder.addInterceptor(new SignInterceptor());
            builder.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS);
            builder.readTimeout(60 * 1000, TimeUnit.MILLISECONDS);
            builder.writeTimeout(60 * 1000, TimeUnit.MILLISECONDS);
            OkHttpClient build = builder.build();
            //retrofit
            Retrofit.Builder builder1 = new Retrofit.Builder();
            builder1.baseUrl(BaseUrl);
            builder1.client(build);
            builder1.addConverterFactory(GsonConverterFactory.create());
            builder1.addCallAdapterFactory(RxJava2CallAdapterFactory.create());

            retrofit = builder1.build();

        }

        //调用此方法创建   不会重复创建
        public UploadRetrofit build() {
            return new UploadRetrofit();
        }

    }

}
